package com.chapter.sample.answer.controller;

import com.chapter.sample.answer.exporter.annotations.MethodExporter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class MethodExporterController {

    @MethodExporter
    @RequestMapping("/method/list")
    public Map list(@RequestParam(value = "page", defaultValue = "1") int page,
                    @RequestParam(value = "rows", defaultValue = "5") int rows) {
        Map<String, String> result = new LinkedHashMap<>();
        result.put("code", "200");
        result.put("message", "success");
        //暂停毫秒
//        try {
//            TimeUnit.SECONDS.sleep(new Random().nextInt(1000));
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return result;
    }


    @MethodExporter
    @RequestMapping(value = "/method/get")
    public Map get() {
        Map<String, String> result = new LinkedHashMap<>();
        result.put("code", "404");
        result.put("message", "not-found");
        //暂停毫秒
        try {
            TimeUnit.SECONDS.sleep(new Random().nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/method/update")
    public String update(){
        log.info("update method without @MethodExporter");
        return "ok,update";
    }
}
